from keras.preprocessing.image import img_to_array
import imutils
import cv2
from keras.models import load_model
import numpy as np
import matplotlib.pyplot as plt
import time
detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml' 
# haarcascade_frontalface_default.xml是一个基于Haar特征的人脸检测器模型，它是OpenCV（一个广泛使用的计算机视觉库）中自带的模型之一
# haarcascade_frontalface_default.xml模型是针对人脸检测的，它能够检测输入图像中的正面人脸，并输出检测到的人脸区域的位置和大小。在很多计算机视觉应用中，如人脸识别、表情识别、人脸跟踪等，人脸检测是必不可少的一步 
emotion_model_path = 'models/_mini_XCEPTION.102-0.66.hdf5' #表情  CNN的主流框架之mini_XCEPTION 有更高性能模型
# fer2013 65%±5% 2013年Kaggle比赛的数据 FER2013数据集数据更加齐全，同时更加符合实际生活的场景
#mini_XCEPTION 是一个卷积神经网络模型架构，最初被提出用于面部表情识别任务。它是 XCEPTION 架构的简化版本，XCEPTION 架构是 François Chollet 
#在2016年提出的深度卷积神经网络。mini_XCEPTION 架构使用深度可分离卷积，这种卷积计算效率高、可以减少模型中的参数数量。
#这使得 mini_XCEPTION 架构适合在计算资源有限的设备上进行实时应用
face_detection = cv2.CascadeClassifier(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
# 并没有用到CNN，而是用的训练好的模型
EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised",
 "neutral"]
emotion_Recod=[]
cv2.namedWindow('video detect')
video_capture=cv2.VideoCapture('models/T.mp4')
# 初始化
plt.ion()
x_data = np.arange(0, 100, 1)
y_happiness = np.zeros((100, ))
y_sadness = np.zeros((100, ))
y_surprise = np.zeros((100, ))
y_anger = np.zeros((100, ))
y_disgust = np.zeros((100, ))
y_fear = np.zeros((100, ))
y_neutral = np.zeros((100, ))
# 绘制竞赛图
line_hap, = plt.plot(x_data, y_happiness, label='Happiness')
line_sad, = plt.plot(x_data, y_sadness, label='Sadness')
line_sur, = plt.plot(x_data, y_surprise, label='Surprise')
line_ang, = plt.plot(x_data, y_anger, label='Anger')
line_dis, = plt.plot(x_data, y_disgust, label='Disgust')
line_fear, = plt.plot(x_data, y_fear, label='Fear')
line_neut, = plt.plot(x_data, y_neutral, label='Neutral')
plt.ylim(0, 100)
plt.xlabel('Time')
plt.ylabel('Probability')
plt.title('CLNS---Emotion Recognition')
plt.legend()




while True:
    ret,frame=video_capture.read()
    if not ret:
        break
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    canvas = np.zeros((250, 300, 3), dtype="uint8")
    faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE) #Haar级联分类器进行面部检测并提取面部区域
    frameClone = frame.copy()
    if len(faces) > 0:
        faces = sorted(faces, reverse=True,
        key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
        (fX, fY, fW, fH) = faces 
        roi = gray[fY:fY + fH, fX:fX + fW] 
        roi = cv2.resize(roi, (64, 64)) 
        roi = roi.astype("float") / 255.0
        roi = img_to_array(roi)
        roi = np.expand_dims(roi, axis=0)
        preds = emotion_classifier.predict(roi)[0] 
        emotion_probability = np.max(preds) 
        label = EMOTIONS[preds.argmax()] 
    else: continue
    for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
          now = time.time()
          text = "'{}': {:.2f}".format(emotion, prob * 100)

          if i == 0:
            print("angry")
            y_anger[:-1] = y_anger[1:]
            y_anger[-1] = prob* 100
            line_ang.set_ydata(y_anger)            
            
          elif i == 1:
            print("disguist")
            y_disgust[:-1] = y_disgust[1:]
            y_disgust[-1] = prob* 100
            line_dis.set_ydata(y_disgust)            
          elif i == 2:
            print("scared")
            y_fear[:-1] = y_fear[1:]
            y_fear[-1] = prob* 100
            line_fear.set_ydata(y_fear)            
          elif i == 3:
            print("happy")
            y_happiness[:-1] = y_happiness[1:]
            y_happiness[-1] = prob* 100
            line_hap.set_ydata(y_happiness)
          elif i == 4:
            print("sad")
            y_sadness[:-1] = y_sadness[1:]
            y_sadness[-1] = prob* 100
            line_sad.set_ydata(y_sadness)            
          elif i == 5:
            print("surprised")
            y_surprise[:-1] = y_surprise[1:]
            y_surprise[-1] = prob* 100
            line_sur.set_ydata(y_surprise)            
          else:
            print("neutral")
            y_neutral[:-1] = y_neutral[1:]
            y_neutral[-1] = prob* 100
            line_neut.set_ydata(y_neutral)            

          plt.draw()
          plt.pause(0.01)
          cv2.putText(frameClone, label, (fX, fY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
          cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH), (0, 0, 255), 2)    
    cv2.imshow('your_face', frameClone)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

plt.show()
plt.ioff()

video_capture.release()
cv2.destroyAllWindows()

